# Contributor: Joe Holden <jwh@zorins.us>
# Contributor: Michał Polański <michal@polanski.me>
# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=traefik
pkgver=3.1.5
pkgrel=0
pkgdesc="The Cloud Native Edge Router"
url="https://traefik.io/"
arch="all"
license="MIT"
makedepends="go"
checkdepends="tzdata"
install="$pkgname.pre-install"
pkgusers="$pkgname"
pkggroups="$pkgname"
subpackages="$pkgname-doc $pkgname-openrc"
options="net setcap chmod-clean"
source="$pkgname-$pkgver.tar.gz::https://github.com/traefik/traefik/releases/download/v$pkgver/traefik-v$pkgver.src.tar.gz
	traefik.initd
	traefik.confd
	traefik.yaml
	"
builddir="$srcdir"

# secfixes:
#   3.1.3-r0:
#     - CVE-2024-45410
#   2.9.10-r0:
#     - CVE-2023-29013
#     - CVE-2023-24534
#   2.9.6-r0:
#     - CVE-2022-23469
#     - CVE-2022-46153
#   2.2.8-r0:
#     - CVE-2020-15129

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	local builddate="$(date -u "+%Y-%m-%dT%TZ" ${SOURCE_DATE_EPOCH:+-d @$SOURCE_DATE_EPOCH})"

	go generate -v
	go build -v -ldflags "-s -w -extldflags \"$LDFLAGS\" \
			-X github.com/traefik/traefik/v3/pkg/version.Codename=cheddar \
			-X github.com/traefik/traefik/v3/pkg/version.Version=$pkgver \
			-X github.com/traefik/traefik/v3/pkg/version.BuildDate=$builddate" \
		-o bin/ ./cmd/$pkgname
}

check() {
	# update fixtures due float values being different on some platforms
	go test ./pkg/provider/traefik/... -update_expected=true

	# skipping:
	# 	logs: failing due "Lshortfile", Im unable to fix
	# 	healthcheck: failing due "30000000000 overflows int" on 32bits platforms
	# 	influxdb2 (part of metrics): failing on riscv64 "context canceled"
	local _filter="logs"
	case "$CARCH" in
		armhf | armv7 | x86 | s390x) _filter="$_filter|healthcheck" ;;
		riscv64) _filter="$_filter|metrics" ;;
	esac

	# shellcheck disable=2046
	go test -timeout 30m -cover "-coverprofile=cover.out" \
		$(go list ./pkg/... ./cmd/... | grep -Ev "pkg/($_filter)")
}

package() {
	install -Dm755 bin/$pkgname "$pkgdir"/usr/sbin/$pkgname
	install -Dm644 LICENSE.md -t "$pkgdir"/usr/share/licenses/$pkgname/
	install -dm775 -o $pkgname -g $pkgname "$pkgdir"/var/log/$pkgname
	install -dm750 -o $pkgname -g $pkgname "$pkgdir"/etc/$pkgname
	install -m644 -o $pkgname -g $pkgname "$srcdir"/traefik.yaml \
		-t "$pkgdir"/etc/$pkgname/

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
875fc2eaa40c96125a7e36510804b7e7d82a8856aeec0e8d60053688c88a720ff357a5fbf4e3e3c0e8a265911f61649f31973b1c964aa8eb4260b6c24218f18c  traefik-3.1.5.tar.gz
d5a64e46ebafebd1352fda2fae5c3b6ccb58d3191583b06efbba9eaf0b444d4a7b6743a347a13ad79ca207ddeda7ab8d7387ffd7d4f9160ec80b5f2521bf8808  traefik.initd
bbc0bb6825f8847ef0ee18b7194afc4e1443041727022e435e48249744b1230b3422ff9d20a6b42d2e3615e57c10254611fe1e999b847e7b4a4d5954bd677cf2  traefik.confd
1a0124b4b312dade4b8aacb6c73ac2898ff5d0e235b8cd1749d8a85a9ef9b383ad5ba6b334335dd3722b1653c6e40fa95690a0e50312f3a133e95704dd1fee36  traefik.yaml
"
